<?php

namespace App\Admin\Actions\Grid;

use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Models\Cabinet;
use App\Handlers\WeconHandler;

class SyncDeviceCainet extends RowAction
{
    /**
     * @return string
     */
	protected $title = '&nbsp;<i class="fa fa-retweet">&nbsp;同步数据</i>&nbsp;';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        // dump($this->getKey());
        // 
        $cabinet = Cabinet::find($this->getKey());
        if (empty($cabinet['machine_code'])) {
            return $this->response()->error('无效的维控机器码');
        }
        Cabinet::syncWeikong($this->getKey());
        return $this->response()->success('同步成功');
        // return $this->response()->success('Processed successfully: '.$this->getKey());
    }

    /**
	 * @return string|array|void
	 */
	public function confirm()
	{
        $cabinet = Cabinet::find($this->getKey());
        $title = '确认同步设备-'.$cabinet['name'].'？';

		return [$title];
	}

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [];
    }
}
